package com.yuzhi.fine.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.yuzhi.fine.R;
import com.yuzhi.fine.activity.MainActivity;
import com.yuzhi.fine.adapter.AllAdapter;
import com.yuzhi.fine.adapter.FuliAdapter;
import com.yuzhi.fine.adapter.NormalAdapter;
import com.yuzhi.fine.http.CallBack;
import com.yuzhi.fine.http.RequestManager;
import com.yuzhi.fine.model.GanHuo;
import com.yuzhi.fine.model.SearchParam;
import com.yuzhi.fine.ui.UIHelper;
import com.yuzhi.fine.ui.loadmore.LoadMoreListView;
import com.yuzhi.fine.utils.DeviceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.header.StoreHouseHeader;

/**
 * Created by tiansj on 15/9/4.
 */
public class ListFragment<T> extends Fragment {
    private MainActivity context;

    private SearchParam param;
    private int mPage = 1;
    private int pageSize = 30;
    private boolean isRefresh;
    private BaseAdapter adapter;
    private List<GanHuo> mGanHuoList = new ArrayList<>();


    @Bind(R.id.rotate_header_list_view_frame)
    PtrClassicFrameLayout mPtrFrame;
    @Bind(R.id.listView)
    LoadMoreListView listView;
    private String mTag = "all";

    public static ListFragment newInstance(String tag) {
        ListFragment fragment = new ListFragment<>();
        Bundle args = new Bundle();
        args.putString("tag", tag);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTag = getArguments().getString("tag");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_demo_ptr, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = (MainActivity) getActivity();
        initData();
        initView();
        loadData();
    }

    void initView() {
        if (mTag.equals("all")) {
            adapter = new AllAdapter(getActivity(), mGanHuoList);
        } else if (mTag.equals("福利")) {
            adapter = new FuliAdapter(getActivity(), mGanHuoList);
        } else {
            adapter = new NormalAdapter(getActivity(), mGanHuoList);
        }
//        listView.setDrawingCacheEnabled(true);
        listView.setAdapter(adapter);

        // header custom begin
        final StoreHouseHeader header = new StoreHouseHeader(context);
        header.setPadding(0, DeviceUtil.dp2px(context, 15), 0, 0);
        header.initWithString("Loofer");
        header.setTextColor(getResources().getColor(R.color.gray));
        mPtrFrame.setHeaderView(header);
        mPtrFrame.addPtrUIHandler(header);
        // header custom end

        // 下拉刷新
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                initData();
                loadData();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });

        // 加载更多
        listView.setOnLoadMoreListener(new LoadMoreListView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isRefresh = false;
                loadData();
            }
        });

        // 点击事件
        listView.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                GanHuo ganHuo = mGanHuoList.get(i);
                if (!ganHuo.getType().equals("福利")) {
                    UIHelper.showDetailActivity(getActivity(), ganHuo);
                } else {
//                    UIHelper.showHouseDetailActivity(context);
                    Toast.makeText(getActivity(), i + "", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void initData() {
        mPage = 1;
        isRefresh = true;
    }

    private void loadData() {
        RequestManager.get(mTag, "http://gank.io/api/data/" + mTag + "/"
                        + pageSize + "/"
                        + mPage, isRefresh,
                new CallBack<List<GanHuo>>() {
                    @Override
                    public void onSuccess(List<GanHuo> result) {
                        if (isRefresh) {
                            mGanHuoList.clear();
                            mPage = 2;
                        } else {
                            mPage++;
                        }
                        mGanHuoList.addAll(result);
                        adapter.notifyDataSetChanged();
                        mPtrFrame.refreshComplete();
                    }

                    @Override
                    public void onFailure(String message) {
                        super.onFailure(message);
                        mPtrFrame.refreshComplete();
                        listView.setLoadMoreViewTextError();
                    }
                });
//        param.setPno(mPage);
//        HttpClient.getRecommendShops(param, new HttpResponseHandler() {
//            @Override
//            public void onSuccess(String body) {
//                mPtrFrame.refreshComplete();
//                JSONObject object = JSON.parseObject(body);
//                List<SearchShop> list = JSONArray.parseArray(object.getString("body"), SearchShop.class);
//                listView.updateLoadMoreViewText(list);
//                isLoadAll = list.size() < HttpClient.PAGE_SIZE;
//                if (mPage == 1) {
//                    adapter.clear();
//                }
//                adapter.addAll(list);
//                mPage++;
//            }
//
//            @Override
//            public void onFailure(Request request, IOException e) {
//                mPtrFrame.refreshComplete();
//                listView.setLoadMoreViewTextError();
//            }
//        });
    }


}
